/**
 * JAVACC DEMO 1.0
 */
package com.apache.passport.common;

import com.apache.api.vo.ParamsVo;
import com.apache.database.constant.SystemTools;
import com.apache.passport.entity.LoginInfo;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * description:  文件数据操作类
 *
 * @author Hou Dayu 创建时间：2016-1-16
 */
public class FileUtils {

    private Logger log = Logger.getLogger(FileUtils.class);

    private static FileUtils instance;

    private String CRLGINFILEPATH = "";

    private FileUtils() {
        /**
         * 文件路径,文件名称为:passport_YYYYMMDD.papt
         * 用户名	userEname
         * 	最后登录介质	lastLgnDevice
         * 	最后登录时间	lastLgnTime
         * 	当前登录时间	currLgnTime
         * 	登录系统名称	SysName
         * 	最后登录IP地址	lastLgnIpAddr
         */
        CRLGINFILEPATH = SystemTools.getInstance().getValue("crLginFilePath");
    }

    public static FileUtils getInstance() {
        if (null == instance) {
            instance = new FileUtils();
        }
        return instance;
    }

    /**
     * description:  创建用户登录日志文件
     *
     * @param param
     * @return
     */
    public boolean createLoginFile(ParamsVo<LoginInfo> param) {
        boolean flag = false;
        try {
            File file = new File(CRLGINFILEPATH);
            if (file.isFile()) {
                log.info("warning: [crLginFilePath非路径],请检查unity_config.properties配置");
            } else {
                if (file.exists()) {
                    flag = file.mkdirs();
                }
                String nowdate = new SimpleDateFormat("yyyyMMdd").format(new Date());
                String fileName = CRLGINFILEPATH + File.separator + "passport_" + nowdate + ".papt";
                file = new File(fileName);
                if (!file.exists()) {
                    file.createNewFile();
                }
                //5M以上,重新生成新文件
                LoginInfo loginInfo = (LoginInfo) param.getObj();
                StringBuilder sb = new StringBuilder();
                sb.append(loginInfo.getSessionId()).append("|");
                sb.append(loginInfo.getTokenId()).append("|");
                sb.append(loginInfo.getUserEname()).append("|");
                sb.append(loginInfo.getLgnLastdevice()).append("|");
                sb.append(loginInfo.getLgnLasttime()).append("|");
                sb.append(loginInfo.getLgnLasttime()).append("|");//注意,当前登录时间暂时为最后登录时间
                sb.append(loginInfo.getSysEname()).append("|");
                sb.append(loginInfo.getLgnLastipaddress()).append("|");
                flag = write(file, sb.toString());
            }
        } catch (IOException e) {
            log.error("创建文件失败：" + e.getMessage());
        }
        return flag;
    }

    /**
     * description:  写本地文件
     *
     * @param fileName
     * @param Content
     * @return
     */
    private boolean write(File fileName, String Content) {
        boolean flag = true;
        FileOutputStream path = null;
        PrintStream p = null;
        try {
            path = new FileOutputStream(fileName, true);
            p = new PrintStream(path);
            p.println(Content);
        } catch (Exception e) {
            flag = false;
        } finally {
            IOUtils.closeQuietly(p);
            IOUtils.closeQuietly(path);
        }
        return flag;
    }

}
